package cn.edu.nju.setting;

import java.util.ArrayList;
import java.util.List;

import com.devsmart.android.HorizontalListView;

import cn.edu.nju.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Window;
import android.widget.LinearLayout.LayoutParams;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class ModifyRuleActivity extends CreateRuleActivity {
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
	}
	
	@Override
	protected void initRuleAndCopyTags(){
		int position = getIntent().getIntExtra(SettingActivity.RULE_ITEM_POSITION, 0);
		rule = settingProvider.getFilterRule(position);
		copyTags = new ArrayList<String>(rule.getTags());
	}
	
	@Override
	protected void initTopLayout(){
		initDeleteButton();
		initSaveButton();
		
		titleText = (TextView)findViewById(R.id.modify_rule_name_text);
		titleText.setText(rule.getRuleName());
	}
	
	private void initDeleteButton(){
		//TODO need affirm
		Button deleteButton = (Button) findViewById(R.id.modify_rule_delete_button);
		deleteButton.setOnClickListener(new OnClickListener() {		
			public void onClick(View v) {
				Intent intent = getIntent();
		        int position = intent.getIntExtra(SettingActivity.RULE_ITEM_POSITION, 0);
		        settingProvider.removeRule(position);
		        settingProvider.setHasRuleDeleted(true);
		        finish();
			}
		});		
	}
	
	@Override
	protected void initSaveButton(){
		Button saveButton = (Button)findViewById(R.id.modify_rule_save_button);
		saveButton.setOnClickListener(new OnClickListener() {		
			public void onClick(View v) {
				if(copyTags.size() > 0){
					rule.setRuleName(ruleNameEdit.getText().toString());				
					rule.setTags(copyTags);
					rule.upDate();
				}
				else
				{
					Toast.makeText(ModifyRuleActivity.this, getString(R.string.tag_empty), Toast.LENGTH_SHORT).show();
				}
			}
		});		
	}
}
